home *** CD-ROM | disk | FTP | other *** search
/ Champak 40 / Vol 40.iso / games / table_so.swf / scripts / frame_68 / DoAction.as
Encoding:
Text File  |  2007-03-21  |  10.4 KB  |  381 lines

  1. function ballMove()
  2. {
  3.    streaks._x = ball._x;
  4.    streaks._y = ball._y;
  5.    gameController();
  6.    if(getTimer() > this.collideTimer + collideTime)
  7.    {
  8.       colliding = false;
  9.    }
  10.    this._rotation += this.dw;
  11.    this.vx *= DAMP;
  12.    this.vy *= DAMP;
  13.    this._x += this.vx;
  14.    this._y += this.vy;
  15.    if(this._x > RIGHT)
  16.    {
  17.       this._x = RIGHT;
  18.       this.vx *= BOUNCE;
  19.       Sound1.start(0,1);
  20.    }
  21.    else if(this._x < LEFT)
  22.    {
  23.       this._x = LEFT;
  24.       this.vx *= BOUNCE;
  25.       Sound1.start(0,1);
  26.    }
  27.    if(this._y > BOTTOM)
  28.    {
  29.       this._y = BOTTOM;
  30.       this.vy *= BOUNCE;
  31.       Sound1.start(0,1);
  32.    }
  33.    else if(this._y < TOP)
  34.    {
  35.       this._y = TOP;
  36.       this.vy *= BOUNCE;
  37.       Sound1.start(0,1);
  38.    }
  39.    if(ball.hitTest(goalOpponent.hit))
  40.    {
  41.       opponentsGoalDisplay.bounceIn(0);
  42.       centerBall();
  43.       scoreOpponent++;
  44.       scoreBoardOpponent.scoreText.text = scoreOpponent;
  45.       if(scoreOpponent == winningScore)
  46.       {
  47.          gotoAndStop("game over");
  48.          play();
  49.       }
  50.    }
  51.    else if(ball.hitTest(goalPlayer.hit))
  52.    {
  53.       playersGoalDisplay.bounceIn(0);
  54.       centerBall();
  55.       scorePlayer++;
  56.       scoreBoardPlayer.scoreText.text = scorePlayer;
  57.       if(scorePlayer == winningScore)
  58.       {
  59.          gotoAndStop("game over");
  60.          play();
  61.       }
  62.    }
  63. }
  64. function centerBall()
  65. {
  66.    ball._visible = true;
  67.    delete ball.onEnterFrame;
  68.    ball._x = 168;
  69.    ball._y = 244;
  70. }
  71. function restart()
  72. {
  73.    centerBall();
  74.    var _loc1_ = new mx.transitions.Tween(ball,"_xscale",mx.transitions.easing.Bounce.easeOut,500,100,1.5,true);
  75.    var _loc2_ = new mx.transitions.Tween(ball,"_yscale",mx.transitions.easing.Bounce.easeOut,500,100,1.5,true);
  76.    _loc1_.onMotionFinished = function()
  77.    {
  78.       ball.dw = 0;
  79.       ball.vx = rand((- VMAX) / 2,VMAX / 2);
  80.       if(Math.random() > 0.5)
  81.       {
  82.          ball.vy = - VMAX;
  83.       }
  84.       else
  85.       {
  86.          ball.vy = VMAX;
  87.       }
  88.       ball.onEnterFrame = ballMove;
  89.    };
  90. }
  91. function gameController()
  92. {
  93.    controls();
  94.    moveOpponent();
  95. }
  96. function controls()
  97. {
  98.    if(Key.isDown(39))
  99.    {
  100.       players._x = Math.min(players._x + MOVERATE,playersRightBoundary);
  101.    }
  102.    else if(Key.isDown(37))
  103.    {
  104.       players._x = Math.max(players._x - MOVERATE,playersLeftBoundary);
  105.    }
  106. }
  107. function moveOpponent()
  108. {
  109.    ballRelative = ball._y - opponents._y;
  110.    if(ball.vy < 0)
  111.    {
  112.       if(ballRelative > rowsOpponent[0][0]._y)
  113.       {
  114.          activeRow = 0;
  115.       }
  116.       else if(ballRelative > rowsOpponent[1][0]._y)
  117.       {
  118.          activeRow = 1;
  119.       }
  120.       else if(ballRelative > rowsOpponent[2][0]._y)
  121.       {
  122.          activeRow = 2;
  123.       }
  124.       else
  125.       {
  126.          activeRow = 3;
  127.       }
  128.    }
  129.    else if(ballRelative < rowsOpponent[3][0]._y)
  130.    {
  131.       activeRow = 3;
  132.    }
  133.    else if(ballRelative < rowsOpponent[2][0]._y)
  134.    {
  135.       activeRow = 2;
  136.    }
  137.    else if(ballRelative < rowsOpponent[1][0]._y)
  138.    {
  139.       activeRow = 1;
  140.    }
  141.    else
  142.    {
  143.       activeRow = 0;
  144.    }
  145.    column = 0;
  146.    distance = Stage.width;
  147.    i = 0;
  148.    while(i < rowsOpponent[activeRow].length)
  149.    {
  150.       testDistance = Math.abs(ball._x - rowsOpponent[activeRow][i]._x - opponents._x);
  151.       if(testDistance < distance)
  152.       {
  153.          column = i;
  154.          distance = testDistance;
  155.       }
  156.       i++;
  157.    }
  158.    rowsOpponent[activeRow][column].tint = 100;
  159.    difference = ball._x - rowsOpponent[activeRow][column]._x - opponents._x;
  160.    if(difference >= OPPONENTMOVERATE)
  161.    {
  162.       opponents._x = Math.min(opponents._x + OPPONENTMOVERATE,opponentsRightBoundary);
  163.    }
  164.    else if(difference <= - OPPONENTMOVERATE)
  165.    {
  166.       opponents._x = Math.max(opponents._x - OPPONENTMOVERATE,opponentsLeftBoundary);
  167.    }
  168.    if(!collisions())
  169.    {
  170.       collisionsPlayer();
  171.    }
  172. }
  173. function aimOpponentGoal()
  174. {
  175.    var _loc2_ = goalOpponent._x - ball._x;
  176.    var _loc1_ = goalOpponent._y - ball._y;
  177.    angle = Math.atan2(_loc1_,_loc2_);
  178.    streaks._rotation = (angle + 3.141592653589793) * 180 / 3.141592653589793;
  179.    streaks.play();
  180.    stick._rotation = angle * 180 / 3.141592653589793;
  181.    return angle;
  182. }
  183. function aimOpponent(target)
  184. {
  185.    var _loc2_ = target._x + opponents._x - ball._x;
  186.    var _loc1_ = target._y + opponents._y - ball._y;
  187.    angle = Math.atan2(_loc1_,_loc2_);
  188.    streaks._rotation = (angle + 3.141592653589793) * 180 / 3.141592653589793;
  189.    streaks.play();
  190.    return angle;
  191. }
  192. function aimPlayerGoal()
  193. {
  194.    var _loc2_ = goalPlayer._x - ball._x;
  195.    var _loc1_ = goalPlayer._y - ball._y;
  196.    angle = Math.atan2(_loc1_,_loc2_);
  197.    streaks._rotation = (angle + 3.141592653589793) * 180 / 3.141592653589793;
  198.    streaks.play();
  199.    angle += Math.random() * 2 * PLAYERGOALRANDOM - PLAYERGOALRANDOM;
  200.    return angle;
  201. }
  202. function aimPlayer(target)
  203. {
  204.    var _loc2_ = target._x + players._x - ball._x;
  205.    var _loc1_ = target._y + players._y - ball._y;
  206.    angle = Math.atan2(_loc1_,_loc2_);
  207.    streaks._rotation = (angle + 3.141592653589793) * 180 / 3.141592653589793;
  208.    streaks.play();
  209.    stick2._rotation = angle * 180 / 3.141592653589793;
  210.    return angle;
  211. }
  212. function collisions()
  213. {
  214.    if(!colliding)
  215.    {
  216.       i = 0;
  217.       while(i < rowsOpponent.length)
  218.       {
  219.          j = 0;
  220.          while(j < rowsOpponent[i].length)
  221.          {
  222.             if(ball.hitTest(rowsOpponent[i][j].hit))
  223.             {
  224.                Sound1.start(0,1);
  225.                rowsOpponent[i][j].gotoAndPlay(2);
  226.                ball.collideTimer = getTimer();
  227.                colliding = true;
  228.                if(i == 0)
  229.                {
  230.                   ballAngle = aimOpponentGoal();
  231.                   ballAngle += Math.random() * ACCURACY - ACCURACY;
  232.                   ball.vx = Math.cos(ballAngle) * VMAXOPPONENT;
  233.                   ball.vy = Math.sin(ballAngle) * VMAXOPPONENT;
  234.                   ball.dw = 2 * ballAngle / 3.141592653589793;
  235.                   return true;
  236.                }
  237.                row = i - 1;
  238.                player = rand(rowsOpponent[row].length - 1);
  239.                mc = rowsOpponent[row][player];
  240.                ballAngle = aimOpponent(mc);
  241.                ballAngle += Math.random() * ACCURACY - ACCURACY;
  242.                ball.vx = Math.cos(ballAngle) * VMAXOPPONENT;
  243.                ball.vy = Math.sin(ballAngle) * VMAXOPPONENT;
  244.                ball.dw = 2 * ballAngle / 3.141592653589793;
  245.                ball._x += ball.vx;
  246.                ball._y += ball.vy;
  247.                return true;
  248.             }
  249.             j++;
  250.          }
  251.          i++;
  252.       }
  253.    }
  254.    return false;
  255. }
  256. function collisionsPlayer()
  257. {
  258.    if(!colliding)
  259.    {
  260.       i = 0;
  261.       while(i < rowsPlayer.length)
  262.       {
  263.          j = 0;
  264.          while(j < rowsPlayer[i].length)
  265.          {
  266.             if(ball.hitTest(rowsPlayer[i][j].hit))
  267.             {
  268.                rowsPlayer[i][j].gotoAndPlay(2);
  269.                Sound1.start(0,1);
  270.                ball.collideTimer = getTimer();
  271.                colliding = true;
  272.                if(i == 0)
  273.                {
  274.                   ballAngle = aimPlayerGoal();
  275.                   ball.vx = Math.cos(ballAngle) * VMAX;
  276.                   ball.vy = Math.sin(ballAngle) * VMAX;
  277.                   ball.dw = 2 * ballAngle / 3.141592653589793;
  278.                   return true;
  279.                }
  280.                row = i - 1;
  281.                player = rand(rowsPlayer[row].length - 1);
  282.                mc = rowsPlayer[row][player];
  283.                ballAngle = aimPlayer(mc);
  284.                ball.vx = Math.cos(ballAngle) * VMAX;
  285.                ball.vy = Math.sin(ballAngle) * VMAX;
  286.                ball.dw = 2 * ballAngle / 3.141592653589793;
  287.                ball._x += ball.vx;
  288.                ball._y += ball.vy;
  289.                return true;
  290.             }
  291.             j++;
  292.          }
  293.          i++;
  294.       }
  295.    }
  296.    return false;
  297. }
  298. MovieClip.prototype.rand = function(num)
  299. {
  300.    if(arguments.length == 1)
  301.    {
  302.       return Math.floor(Math.random() * num) + 1;
  303.    }
  304.    return Math.floor(Math.random() * (Math.abs(arguments[1] - num) + 1)) + Math.min(num,arguments[1]);
  305. };
  306. Sound1 = new Sound();
  307. Sound1.attachSound("soccer_01");
  308. Sound1.setVolume(10);
  309. GAME_ANGLE = 0;
  310. BALL_DIAMETER = 12;
  311. BALL_RADIUS = BALL_DIAMETER / 2;
  312. TOP = table_mc._y + BALL_RADIUS;
  313. BOTTOM = table_mc._y + table_mc._height - BALL_RADIUS;
  314. LEFT = table_mc._x + BALL_RADIUS;
  315. RIGHT = table_mc._x + table_mc._width - BALL_RADIUS;
  316. BOUNCE = -1;
  317. DAMP = 1;
  318. MINSPEED = 1;
  319. var playersLeftBoundary = 120;
  320. var playersRightBoundary = 240;
  321. var opponentsLeftBoundary = 10;
  322. var opponentsRightBoundary = 140;
  323. var scorePlayer = 0;
  324. var scoreOpponent = 0;
  325. var deg2rad = 0.017453292519943295;
  326. var colliding = false;
  327. var collideTime = 100;
  328. var winningScore = 5;
  329. ball._visible = false;
  330. goalPaddle._visible = false;
  331. goalCyclone._visible = false;
  332. trace(_global.player);
  333. if(_global.player == "paddle")
  334. {
  335.    VMAX = 6;
  336.    VMAXOPPONENT = 5;
  337.    ACCURACY = 30 * deg2rad;
  338.    MOVERATE = 3;
  339.    OPPONENTMOVERATE = 1.5;
  340.    PLAYERGOALRANDOM = 10 * deg2rad;
  341.    opponents.gotoAndPlay("cyclone");
  342.    players.gotoAndPlay("paddle");
  343.    scoreBoardOpponent.logo.gotoAndPlay("cyclone");
  344.    scoreBoardPlayer.logo.gotoAndPlay("paddle");
  345.    opponentsGoalDisplay = goalCyclone;
  346.    playersGoalDisplay = goalPaddle;
  347. }
  348. else
  349. {
  350.    VMAX = 5;
  351.    VMAXOPPONENT = 6;
  352.    ACCURACY = 10 * deg2rad;
  353.    MOVERATE = 3;
  354.    OPPONENTMOVERATE = 1;
  355.    PLAYERGOALRANDOM = 10 * deg2rad;
  356.    opponents.gotoAndPlay("paddle");
  357.    players.gotoAndPlay("cyclone");
  358.    scoreBoardOpponent.logo.gotoAndPlay("paddle");
  359.    scoreBoardPlayer.logo.gotoAndPlay("cyclone");
  360.    opponentsGoalDisplay = goalPaddle;
  361.    playersGoalDisplay = goalCyclone;
  362. }
  363. scoreBoardOpponent.slide(0,restart);
  364. scoreBoardPlayer.slide(400);
  365. Color.prototype.reduceRGB = function(p)
  366. {
  367.    var _loc2_ = this.getTransform();
  368.    var _loc3_ = (100 - p) / 100;
  369.    this.setTransform({ra:p,rb:_loc2_.rb * _loc3_,ga:p,gb:_loc2_.gb * _loc3_,ba:p,bb:_loc2_.bb * _loc3_});
  370. };
  371. Color.prototype.tintRGB = function(hex, p)
  372. {
  373.    this.setRGB(hex);
  374.    this.reduceRGB(p);
  375. };
  376. var rowsOpponent = [[opponents.ball0,opponents.ball1,opponents.ball2],[opponents.ball3,opponents.ball4,opponents.ball6,opponents.ball7],[opponents.ball8,opponents.ball9],[opponents.ball10]];
  377. var rowsPlayer = [[players.ball0,players.ball1,players.ball2],[players.ball3,players.ball4,players.ball6,players.ball7],[players.ball8,players.ball9],[players.ball10]];
  378. trace(scoreOpponent);
  379. scoreBoardOpponent.setScore(1);
  380. stop();
  381.